import json
import requests
from utils.logger import log


def get_code():
    import random
    s_code = ''
    for i in range(4):
        s_code += str(random.randint(0, 9))
    print(s_code)
    return s_code


def send_single_sms(mobile, sms_code):
    from . import settings
    # 发送单条短信
    url = settings.url
    text = f"您的验证码是{sms_code}。如非本人操作，请忽略本短信"
    apikey = settings.apikey
    try:
        res = requests.post(url, data={
            "apikey": apikey,
            "mobile": mobile,
            "text": text
        })

        re_json = json.loads(res.text)
        print(re_json)
        re = re_json['code']
        print(re)
        if re == 5:
            return False
        else:
            return True
    except Exception as e:
        log.error('手机号：%s,短信发送失败,错误为：%s' % (mobile, str(e)))
